package com.winning.sx.microframework.service.base.impl;

import com.winning.sx.microframework.dao.EasyDao;
import com.winning.sx.microframework.model.exception.CustomException;
import com.winning.sx.microframework.service.base.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EquipmentServiceImpl implements EquipmentService {
    @Autowired
    private EasyDao dao;

    @Override
    public Map<String ,String> getOrgname(String id){
        try {
            Map<String ,String> resultMap=new HashMap();
            //获取deptname
            Map param=new HashMap();
            param.put("id",id);
            String sql = "select * " +
                    " from Base_organization o where  id in (select organizationId from BASE_Equipment where id=:id)";
            Map result = dao.querySql(sql, param).getDataRows().get(0);
            String deptName=result.get("name").toString();
            String superName="";
            if(!result.get("superOrganizationId").toString().equals("0")){
                superName=getParentName(result.get("superOrganizationId").toString(),"");
            }else{
                superName=deptName;
            }
            resultMap.put("deptName",deptName);
            resultMap.put("superName",superName);
            resultMap.put("topName",superName.split("-")[0]);

            return resultMap;
        } catch (SQLException e) {
            return null;
        }
    }
    private String getParentName(String id,String name) throws SQLException {
        Map param=new HashMap();
        param.put("id",id);
        String sql = "select * " +
                " from Base_organization o where id=:id";
        Map result = dao.querySql(sql, param).getDataRows().get(0);
        name=result.get("name").toString()+"-"+name;
        if(!result.get("superOrganizationId").toString().equals("0")){
            name=getParentName(result.get("superOrganizationId").toString(),name)+"-"+name;
        }
        return name;
    }


}
